package com.chacha.framework.dasboard.tools;
/**
 * 
 */


import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

/**
 * @author rachmat_doni@smltech.co.id
 * 
 */

public class DataJsonMultiSeries {

	private List<String> properties;
	private List<String> categorys;
	private List<Data> datasets;
	private List<TrendLine> trendLines;
	private List<VLine> vLines;
	
	public DataJsonMultiSeries(List<String> properties){
		this.properties = properties;
		categorys = new ArrayList<String>();
		datasets = new ArrayList<Data>();
		trendLines = new ArrayList<>();
		vLines = new ArrayList<>();
	}
	
	public void addData(String label, String seriesName, String parentYAxis,
			String renderAs, String value, String url){
		boolean exist = false;
		// create category
		for(String ctg: categorys){
			if(ctg.equals(label)){
				exist = true;
			}
		}
		if(!exist){
			categorys.add(label);
		}
		
		// create dataset
		exist = false;
		for(Data data: datasets)
			if(data.getDataset().equalsIgnoreCase(seriesName)){
				data.getValue().add(value);
				data.getLink().add(url);
				exist = true;
			}
		
		if(!exist){
			Data data = new Data(seriesName, parentYAxis, renderAs,
					new ArrayList<String>(), new ArrayList<String>());
			data.getValue().add(value);
			data.getLink().add(url);
			datasets.add(data);
			
		}

	}
	
	public void addVLine(int insert, List<String> props){
		vLines.add(new VLine(insert, props));
	}
	
	public void addBaseLine(List<String> properties){
		boolean isExist = false;
		for(String prop: properties){
			for(TrendLine line: trendLines){
				for(String prop1: line.getProperties()){
					if(StringUtils.containsIgnoreCase(prop1, "displayvalue") 
							&& prop1.equalsIgnoreCase(prop)){
						isExist = true;
					}
				}
			}
		}
		if(!isExist)
		trendLines.add(new TrendLine(properties));
	}
	
	public String getJSONData(){
		StringBuilder initBuil  = new StringBuilder("{   \"chart\": {");
		StringBuilder trendlineBuil  = new StringBuilder(",\"trendlines\":[");
		StringBuilder categoryBuil = new StringBuilder("\"categories\":[{\"category\":["); 
		StringBuilder datasetBuil = new StringBuilder("\"dataset\":["); 
		
		for(String prop: properties){
			initBuil.append(prop +", ");
		}
		initBuil.delete(initBuil.length()-2, initBuil.length());
		initBuil.append("}, ");

		int count = 0;
		for(String label: categorys){
			categoryBuil.append("{ \"label\": \""+label+"\" },");
			
			if(vLines.size()>0){
				for(VLine line: vLines){
					if(line.getInsert() == count){
						categoryBuil.append("{ \"vline\":\"true\", ");
						for(String val: line.getProperties()){
							categoryBuil.append(val+",");
						}
						categoryBuil.delete(categoryBuil.length()-1, categoryBuil.length());
						categoryBuil.append(" },");
					}
				}
				count ++;
			}
		}
		categoryBuil.delete(categoryBuil.length()-1, categoryBuil.length());
		categoryBuil.append("]} ],");
		
		for(Data data: datasets){
			datasetBuil.append("{ \"seriesname\":\""+data.getDataset()+"\", ") ;
			
			if(data.getParentYAxis() != null){
				datasetBuil.append("\"parentyaxis\": \""+data.getParentYAxis()+"\", ");
			}
			if(data.getRenderAs() != null){
				datasetBuil.append("\"renderas\": \""+data.getRenderAs()+"\", ");
			}
			
			datasetBuil.append("\"data\":[");
			
			for(int i=0; i<data.getValue().size(); i++){
				datasetBuil.append("{ \"value\": \""+data.getValue().get(i)	+"\" ");
				if(data.getLink().get(i)!=null && !data.getLink().get(i).isEmpty())
					datasetBuil.append(",\"link\": \""+data.getLink().get(i)+"\"");
				
				datasetBuil.append("},");
			}
			datasetBuil.delete(datasetBuil.length()-1, datasetBuil.length());
			datasetBuil.append("]},");
		}
		datasetBuil.delete(datasetBuil.length()-1, datasetBuil.length());
		datasetBuil.append("]");
		
		if(trendLines.size()>0){
			trendlineBuil.append("{ \"line\":[");
			for(TrendLine line: trendLines){
				trendlineBuil.append("{ ");
				for(String prop: line.getProperties()){
					trendlineBuil.append(prop + ",");
				}
				trendlineBuil.delete(trendlineBuil.length()-1, trendlineBuil.length());
				trendlineBuil.append(" },");
			}
			trendlineBuil.delete(trendlineBuil.length()-1, trendlineBuil.length());
			trendlineBuil.append("]}]");
		} else
			trendlineBuil  = new StringBuilder();
		
		if(categorys.size()>0){
			initBuil.append(categoryBuil.toString());
			initBuil.append(datasetBuil.toString());
		}else{
			initBuil.delete(initBuil.length()-2, initBuil.length());
		}
		
		initBuil.append(trendlineBuil.toString()+"}");
		
		return initBuil.toString(); 
	}
	
	private class Data{
		private String dataset;
		private String parentYAxis;
		private String renderAs;
		private List<String> value;
		private List<String> link;
		
		public Data(String dataset, String parentYAxis, String renderAs, 
				List<String> value, List<String> link) {
			super();
			this.parentYAxis = parentYAxis;
			this.renderAs = renderAs;
			this.dataset = dataset;
			this.value = value;
			this.link = link;
		}
		
		public String getRenderAs() {
			return renderAs;
		}

		public String getParentYAxis() {
			return parentYAxis;
		}

		public List<String> getValue() {
			return value;
		}
		public List<String> getLink() {
			return link;
		}
		public String getDataset() {
			return dataset;
		}
		
	}
	
	private class TrendLine{
		private List<String> properties;		
		
		public TrendLine(List<String> properties) {
			super();
			this.properties = properties;
		}

		public List<String> getProperties() {
			return properties;
		}
		
	}
	
	private class VLine{
		private int insert;
		private List<String> properties;		
		
		public VLine(int insert, List<String> properties) {
			super();
			this.insert = insert;
			this.properties = properties;
		}

		public int getInsert() {
			return insert;
		}

		public List<String> getProperties() {
			return properties;
		}
		
	}
}
